﻿using Common.Interfaces;
using Domain.Roles.Exceptions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

namespace Domain.Roles.ValueObjects
{
    /// <summary>
    /// 角色名称
    /// </summary>
    public class RoleName : ValueObject
    {
        public string Value { get; init; }

        private RoleName(string name) => Value = name;

        /// <summary>
        /// 实例化角色名称
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        /// <exception cref="InvalidRoleNameException"></exception>
        public static RoleName Create(string name)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new InvalidRoleNameException("角色名称不能为空");
            }

            if (name.Length > 255)
            {
                throw new InvalidRoleNameException("角色名称太长！最大不能超过255");
            }

            return new RoleName(name);
        }

        protected override IEnumerable<object> GetEqualityComponents()
        {
            yield return Value;
        }

        public override string ToString()
        {
            return Value;
        }

        public static implicit operator string(RoleName name) => name.Value;
    }
}
